home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Interfaces&Libraries / Universal / Interfaces / CIncludes / DigitalSignature.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-17  |  16.3 KB  |  396 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        DigitalSignature.h
  3.  
  4.      Contains:    Digital Signature Interfaces.
  5.  
  6.      Version:    Technology:    AOCE toolbox 1.02
  7.                  Release:    Universal Interfaces 3.2
  8.  
  9.      Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved.
  10.  
  11.      Bugs?:        For bug reports, consult the following page on
  12.                  the World Wide Web:
  13.  
  14.                      http://developer.apple.com/bugreporter/
  15.  
  16. */
  17. #ifndef __DIGITALSIGNATURE__
  18. #define __DIGITALSIGNATURE__
  19.  
  20. #ifndef __MACTYPES__
  21. #include <MacTypes.h>
  22. #endif
  23. #ifndef __MIXEDMODE__
  24. #include <MixedMode.h>
  25. #endif
  26. #ifndef __FILES__
  27. #include <Files.h>
  28. #endif
  29.  
  30.  
  31.  
  32. #if PRAGMA_ONCE
  33. #pragma once
  34. #endif
  35.  
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39.  
  40. #if PRAGMA_IMPORT
  41. #pragma import on
  42. #endif
  43.  
  44. #if PRAGMA_STRUCT_ALIGN
  45.     #pragma options align=mac68k
  46. #elif PRAGMA_STRUCT_PACKPUSH
  47.     #pragma pack(push, 2)
  48. #elif PRAGMA_STRUCT_PACK
  49.     #pragma pack(2)
  50. #endif
  51.  
  52. /* values of SIGNameAttributeType */
  53.  
  54. enum {
  55.     kSIGCountryCode                = 0,
  56.     kSIGOrganization            = 1,
  57.     kSIGStreetAddress            = 2,
  58.     kSIGState                    = 3,
  59.     kSIGLocality                = 4,
  60.     kSIGCommonName                = 5,
  61.     kSIGTitle                    = 6,
  62.     kSIGOrganizationUnit        = 7,
  63.     kSIGPostalCode                = 8
  64. };
  65.  
  66. typedef unsigned short                     SIGNameAttributeType;
  67. /* 
  68. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  69. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  70. the certificate is currently valid. kSIGPending means the certificate is
  71. currently not valid - but will be.  kSIGExpired means the certificate has
  72. expired. A time is always associated with a SIGCertStatus.  In each case the
  73. time has a specific interpretation.  When the status is kSIGValid the time is
  74. when the certificate will expire. When the status is kSIGPending the time is
  75. when the certificate will become valid. When the status is kSIGExpired the time
  76. is when the certificate expired. In the SIGCertInfo structure, the startDate
  77. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  78. structure, this information is provided in the certSetStatusTime field. In the
  79. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  80. field which can contain any of the types below. NOTE: The only time you will get 
  81. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  82. you get a signature that was created after the certificates expiration date, something
  83. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  84. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  85. possibly allowed by other platforms.
  86. */
  87. /* Values for SIGCertStatus or SIGSignatureStatus */
  88.  
  89. enum {
  90.     kSIGValid                    = 0,                            /* possible for either a SIGCertStatus or SIGSignatureStatus */
  91.     kSIGPending                    = 1,                            /* possible for either a SIGCertStatus or SIGSignatureStatus */
  92.     kSIGExpired                    = 2,                            /* possible for either a SIGCertStatus or SIGSignatureStatus * possible only for a SIGSignatureStatus */
  93.     kSIGInvalid                    = 3
  94. };
  95.  
  96. typedef unsigned short                     SIGCertStatus;
  97. typedef unsigned short                     SIGSignatureStatus;
  98. /* Number of bytes needed for a digest record when using SIGDigest */
  99.  
  100. enum {
  101.     kSIGDigestSize                = 16
  102. };
  103.  
  104. typedef Byte                             SIGDigestData[16];
  105. typedef Byte *                            SIGDigestDataPtr;
  106.  
  107. struct SIGCertInfo {
  108.     unsigned long                     startDate;                    /* cert start validity date */
  109.     unsigned long                     endDate;                    /* cert end validity date */
  110.     SIGCertStatus                     certStatus;                    /* see comment on SIGCertStatus for definition */
  111.     unsigned long                     certAttributeCount;            /* number of name attributes in this cert */
  112.     unsigned long                     issuerAttributeCount;        /* number of name attributes in this certs issuer */
  113.     Str255                             serialNumber;                /* cert serial number */
  114. };
  115. typedef struct SIGCertInfo                SIGCertInfo;
  116. typedef SIGCertInfo *                    SIGCertInfoPtr;
  117.  
  118. struct SIGSignerInfo {
  119.     unsigned long                     signingTime;                /* time of signing */
  120.     unsigned long                     certCount;                    /* number of certificates in the cert set */
  121.     unsigned long                     certSetStatusTime;            /* Worst cert status time. See comment on SIGCertStatus for definition */
  122.     SIGSignatureStatus                 signatureStatus;            /* The status of the signature. See comment on SIGCertStatus for definition*/
  123. };
  124. typedef struct SIGSignerInfo            SIGSignerInfo;
  125. typedef SIGSignerInfo *                    SIGSignerInfoPtr;
  126.  
  127. struct SIGNameAttributesInfo {
  128.     Boolean                         onNewLevel;
  129.     Boolean                         filler1;
  130.     SIGNameAttributeType             attributeType;
  131.     ScriptCode                         attributeScript;
  132.     Str255                             attribute;
  133. };
  134. typedef struct SIGNameAttributesInfo    SIGNameAttributesInfo;
  135. typedef SIGNameAttributesInfo *            SIGNameAttributesInfoPtr;
  136.  
  137. typedef Ptr                             SIGContextPtr;
  138. typedef Ptr                             SIGSignaturePtr;
  139. /*
  140. Certificates are always in order. That is, the signers cert is always 0, the
  141. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  142. You can use this constant for readability in your code.
  143. */
  144.  
  145. enum {
  146.     kSIGSignerCertIndex            = 0
  147. };
  148.  
  149. /*
  150. Call back procedure supplied by developer, return false to cancel the current
  151. process.
  152. */
  153. typedef CALLBACK_API( Boolean , SIGStatusProcPtr )(void );
  154. typedef STACK_UPP_TYPE(SIGStatusProcPtr)                         SIGStatusUPP;
  155. enum { uppSIGStatusProcInfo = 0x00000010 };                     /* pascal 1_byte Func() */
  156. #define NewSIGStatusProc(userRoutine)                             (SIGStatusUPP)NewRoutineDescriptor((ProcPtr)(userRoutine), uppSIGStatusProcInfo, GetCurrentArchitecture())
  157. #define CallSIGStatusProc(userRoutine)                             CALL_ZERO_PARAMETER_UPP((userRoutine), uppSIGStatusProcInfo)
  158. /*
  159. Resource id's of standard signature icon suite, all sizes and colors are available.
  160. */
  161.  
  162. enum {
  163.     kSIGSignatureIconResID        = -16797,
  164.     kSIGValidSignatureIconResID    = -16799,
  165.     kSIGInvalidSignatureIconResID = -16798
  166. };
  167.  
  168. /* ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— 
  169. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  170. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  171. a context for you.  To free the memory occupied by the context and invalidate
  172. its internal data, call SIGDisposeContext. An initialized context has no notion
  173. of the type of operation it will be performing however, once you call
  174. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  175. type is set and to switch  to another type of operation will require creating a
  176. new context. Be sure to pass the same context to corresponding toolbox calls
  177. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  178. signing and verify calls with the same context is not allowed.
  179. */
  180. EXTERN_API( OSErr )
  181. SIGNewContext                    (SIGContextPtr *        context)                            FOURWORDINLINE(0x203C, 0x0002, 0x076C, 0xAA5D);
  182.  
  183. EXTERN_API( OSErr )
  184. SIGDisposeContext                (SIGContextPtr             context)                            FOURWORDINLINE(0x203C, 0x0002, 0x076D, 0xAA5D);
  185.  
  186.  
  187. /* ——————————————————————————————— SIGNING CALLS ——————————————————————————————— 
  188. Once you have created a SIGContextPtr, you create a signature by calling
  189. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  190. toRcpt SIGSign. To create another signature on different data but for the same
  191. signer, don't dispose of the context and call SIGProcessData for the new data
  192. followed by a call SIGSign again. In this case the signer will not be prompted
  193. for their signer and password again as it was already provided.  Once you call
  194. SIGDisposeContext, all signer information will be cleared out of the context and
  195. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  196. if you want the toolbox to use the last signer by default or prompt for a signer
  197. if none exists.  The prompt parameter can be used to pass a string to be displayed
  198. in the dialog that prompts the user for their password.  If the substring "^1"
  199. (without the quotes) is in the prompt string, then the toolbox will replace it
  200. with the name of the signer from the signer selected by the user.  If an empty
  201. string is passed, the following default string will be sent to the toolbox
  202. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  203. or SIGSign to get information about the signer or certs.
  204. */
  205. EXTERN_API( OSErr )
  206. SIGSignPrepare                    (SIGContextPtr             context,
  207.                                  const FSSpec *            signerFile,
  208.                                  ConstStr255Param         prompt,
  209.                                  Size *                    signatureSize)                        FOURWORDINLINE(0x203C, 0x0008, 0x076E, 0xAA5D);
  210.  
  211. EXTERN_API( OSErr )
  212. SIGSign                            (SIGContextPtr             context,
  213.                                  SIGSignaturePtr         signature,
  214.                                  SIGStatusUPP             statusProc)                            FOURWORDINLINE(0x203C, 0x0006, 0x076F, 0xAA5D);
  215.  
  216.  
  217. /* ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— 
  218. Once you have created a SIGContextPtr, you verify a signature by calling
  219. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  220. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  221. verified or not (noErr is returned on  success otherwise the appropriate error
  222. code).  Upon successfull verification, you can call any of the utility routines
  223. toRcpt find out who signed the data.
  224. */
  225. EXTERN_API( OSErr )
  226. SIGVerifyPrepare                (SIGContextPtr             context,
  227.                                  SIGSignaturePtr         signature,
  228.                                  Size                     signatureSize,
  229.                                  SIGStatusUPP             statusProc)                            FOURWORDINLINE(0x203C, 0x0008, 0x0770, 0xAA5D);
  230.  
  231. EXTERN_API( OSErr )
  232. SIGVerify                        (SIGContextPtr             context)                            FOURWORDINLINE(0x203C, 0x0002, 0x0771, 0xAA5D);
  233.  
  234. /* —————————————————————————————— DIGESTING CALLS —————————————————————————————— 
  235. Once you have created a SIGContextPtr, you create a digest by calling
  236. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  237. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  238. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  239. digest.
  240. */
  241. EXTERN_API( OSErr )
  242. SIGDigestPrepare                (SIGContextPtr             context)                            FOURWORDINLINE(0x203C, 0x0002, 0x0772, 0xAA5D);
  243.  
  244. EXTERN_API( OSErr )
  245. SIGDigest                        (SIGContextPtr             context,
  246.                                  SIGDigestData             digest)                                FOURWORDINLINE(0x203C, 0x0004, 0x0773, 0xAA5D);
  247.  
  248.  
  249. /* —————————————————————————————— PROCESSING DATA —————————————————————————————— 
  250. To process data during a digest, sign, or verify operation call SIGProcessData
  251. as many times as necessary and with any sized blocks of data.  The data needs to
  252. be processed in the same order during corresponding sign and verify operations
  253. but does not need to be processed in the same sized chunks (i.e., the toolbox
  254. just sees it as a continuous bit stream).
  255. */
  256. EXTERN_API( OSErr )
  257. SIGProcessData                    (SIGContextPtr             context,
  258.                                  const void *            data,
  259.                                  Size                     dataSize)                            FOURWORDINLINE(0x203C, 0x0006, 0x0774, 0xAA5D);
  260.  
  261.  
  262. /* ——————————————————————————————— UTILITY CALLS ——————————————————————————————— 
  263. Given a context that has successfully performed a verification SIGShowSigner
  264. will  display a modal dialog with the entire distinguished name of the person
  265. who signed the data. the prompt (if supplied) will appear at the top of the
  266. dialog.  If no prompt is specified, the default prompt "\pVerification
  267. Successfull." will appear.
  268.  
  269. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  270. successful SIGVerify, you can make the remaining utility calls:
  271.  
  272. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  273. toRcpt index into the certificate set when calling SIGGetCertInfo,
  274. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  275. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  276. will tell you the best status of the entire certificate set while
  277. certSetStatusTime will correspond to the time associated with that status (see
  278. definitions above).
  279.  
  280. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  281. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  282. always the signers certificate.  The  serial number, start date and end date
  283. are there should you wish to display that info.  The  certAttributeCount and
  284. issuerAttributeCount provide the number of parts in the name of that certificate
  285. or that certificates issuer respectively.  You use these numbers to index into
  286. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  287. the name. The certStatus will tell you the status of the certificate while
  288. certStatusTime will correspond to the time associated with that status (see
  289. definitions above).
  290.  
  291. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  292. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  293. tells you wether the name attribute returned is at the same level in the name
  294. hierarchy as the previous attribute.  The type return value tells you  the type
  295. of attribute returned. nameAttribute is the actual string containing the name
  296. attribute.   So, if you wanted to display the entire distinguished name of the
  297. person who's signature was just validated you could do something like this;
  298.  
  299.     (…… variable declarations and verification code would preceed this sample ……)
  300.  
  301.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  302.     HandleErr(error);
  303.  
  304.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  305.         {
  306.         error = SIGGetCertNameAttributes(
  307.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  308.         HandleErr(error);
  309.         DisplayNamePart(theAttribute, type, newLevel);
  310.         }
  311. */
  312. EXTERN_API( OSErr )
  313. SIGShowSigner                    (SIGContextPtr             context,
  314.                                  ConstStr255Param         prompt)                                FOURWORDINLINE(0x203C, 0x0004, 0x0775, 0xAA5D);
  315.  
  316. EXTERN_API( OSErr )
  317. SIGGetSignerInfo                (SIGContextPtr             context,
  318.                                  SIGSignerInfo *        signerInfo)                            FOURWORDINLINE(0x203C, 0x0004, 0x0776, 0xAA5D);
  319.  
  320. EXTERN_API( OSErr )
  321. SIGGetCertInfo                    (SIGContextPtr             context,
  322.                                  unsigned long             certIndex,
  323.                                  SIGCertInfo *            certInfo)                            FOURWORDINLINE(0x203C, 0x0006, 0x0777, 0xAA5D);
  324.  
  325. EXTERN_API( OSErr )
  326. SIGGetCertNameAttributes        (SIGContextPtr             context,
  327.                                  unsigned long             certIndex,
  328.                                  unsigned long             attributeIndex,
  329.                                  SIGNameAttributesInfo * attributeInfo)                        FOURWORDINLINE(0x203C, 0x0008, 0x0778, 0xAA5D);
  330.  
  331. EXTERN_API( OSErr )
  332. SIGGetCertIssuerNameAttributes    (SIGContextPtr             context,
  333.                                  unsigned long             certIndex,
  334.                                  unsigned long             attributeIndex,
  335.                                  SIGNameAttributesInfo * attributeInfo)                        FOURWORDINLINE(0x203C, 0x0008, 0x0779, 0xAA5D);
  336.  
  337.  
  338.  
  339. /* ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— 
  340. These calls allow you to detect the presence of a standard signtaure in a file as 
  341. well as sign and verify files in a standard way.  An example of this is the Finder, 
  342. which uses these calls to allow the user to "drop sign" a file.
  343.  
  344. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  345. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  346. be returned.
  347.  
  348. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  349. a standard way: 
  350.  
  351. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  352. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  353. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  354. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  355. it is replaced with the newly created signature.
  356.  
  357. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  358. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  359. to SIGShowSigner to display the name of the person who signed the file.
  360. */
  361. EXTERN_API( OSErr )
  362. SIGFileIsSigned                    (const FSSpec *            fileSpec)                            FOURWORDINLINE(0x203C, 0x0002, 0x09C4, 0xAA5D);
  363.  
  364. EXTERN_API( OSErr )
  365. SIGSignFile                        (SIGContextPtr             context,
  366.                                  Size                     signatureSize,
  367.                                  const FSSpec *            fileSpec,
  368.                                  SIGStatusUPP             statusProc)                            FOURWORDINLINE(0x203C, 0x0008, 0x09C5, 0xAA5D);
  369.  
  370. EXTERN_API( OSErr )
  371. SIGVerifyFile                    (SIGContextPtr             context,
  372.                                  const FSSpec *            fileSpec,
  373.                                  SIGStatusUPP             statusProc)                            FOURWORDINLINE(0x203C, 0x0006, 0x09C6, 0xAA5D);
  374.  
  375.  
  376. #if PRAGMA_STRUCT_ALIGN
  377.     #pragma options align=reset
  378. #elif PRAGMA_STRUCT_PACKPUSH
  379.     #pragma pack(pop)
  380. #elif PRAGMA_STRUCT_PACK
  381.     #pragma pack()
  382. #endif
  383.  
  384. #ifdef PRAGMA_IMPORT_OFF
  385. #pragma import off
  386. #elif PRAGMA_IMPORT
  387. #pragma import reset
  388. #endif
  389.  
  390. #ifdef __cplusplus
  391. }
  392. #endif
  393.  
  394. #endif /* __DIGITALSIGNATURE__ */
  395.  
  396.